package com.ys.web.yinsheng.util;

import com.ys.utils.base.j2se.JsonUtil;
import com.ys.utils.base.j2se.MapHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.spring.config.SystemError;
import com.ys.web.yinsheng.model.RequestData;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: YsSignUtils
 * @Date: 2022/8/9 20:13
 * @Description:
 * @Version: 1.0
 */
@Slf4j
public class YsSignUtils {
    // 响应结果包含签名
    public static Map<String, String> buildResponseSign(String privateCerPwd, String privateCerPath,
                                                        RequestData baseReqCommand) {
        Map<String, String> reqHeadMap = MapHandler.obj2map1(baseReqCommand);
        // 除去数组中的空值和签名参数
        Map<String, String> sPara = SignUtils.paraFilter(reqHeadMap);
        /* 通过file读取证书 */
        File file = new File(privateCerPath);
        try {
            InputStream pfxCertFileInputStream = new FileInputStream(file);
            // 遍历以及根据重新排序
            String signContent = SignUtils.getSignContent(sPara);
            String sign = SignUtils.rsaSign(signContent,
                    reqHeadMap.get("charset") == null ? "UTF-8" : reqHeadMap.get("charset"), privateCerPwd,
                    pfxCertFileInputStream, privateCerPath);
            sPara.put("sign", sign);
            return sPara;
        } catch (Exception e) {
            log.error("CHANNEL_SIGN_FAIL,签名失败，请检查证书文件是否存在，密码是否正确");
            SystemError.wrapBs("500", "签名失败");
        }
        return null;
    }
    public static Map<String, String> buildResponseSign(String privateCerPwd, String privateCerPath,
                                                        RequestData baseReqCommand,Map<String,Object> requestMap) {
        Map<String, String> reqHeadMap = MapHandler.obj2map1(baseReqCommand);
        // 除去数组中的空值和签名参数
        Map<String, String> sPara = SignUtils.paraFilter(reqHeadMap);
        /* 通过file读取证书 */
        File file = new File(privateCerPath);
        try {
            InputStream pfxCertFileInputStream = new FileInputStream(file);
            // 遍历以及根据重新排序
            String signContent = SignUtils.getSignContent(sPara);
            String sign = SignUtils.rsaSign(signContent,
                    reqHeadMap.get("charset") == null ? "UTF-8" : reqHeadMap.get("charset"), privateCerPwd,
                    pfxCertFileInputStream, privateCerPath);
            sPara.put("sign", sign);
            sPara.put("bizReqJson", JsonUtil.toJson(requestMap));
            return sPara;
        } catch (Exception e) {
            log.error("CHANNEL_SIGN_FAIL,签名失败，请检查证书文件是否存在，密码是否正确");
            SystemError.wrapBs("500", "签名失败");
        }
        return null;
    }
    // 响应结果包含签名
    public static Map<String, String> buildResponseSign(String privateCerPwd, String privateCerPath,
                                                        Map<String, String> reqHeadMap) {
        System.out.println("reqHeadMap==" + reqHeadMap);
        // 除去数组中的空值和签名参数
        Map<String, String> sPara = SignUtils.paraFilter(reqHeadMap);
        System.out.println("sPara==" + sPara);
        /* 通过file读取证书 */
        File file = new File(privateCerPath);
        try {
            InputStream pfxCertFileInputStream = new FileInputStream(file);
            // 遍历以及根据重新排序
            String signContent = SignUtils.getSignContent(sPara);

            System.out.println("signContent==" + signContent);

            String sign = SignUtils.rsaSign(signContent,
                    reqHeadMap.get("charset") == null ? "UTF-8" : reqHeadMap.get("charset"), privateCerPwd,
                    pfxCertFileInputStream, privateCerPath);
            sPara.put("sign", sign);
            return sPara;
        } catch (Exception e) {
            log.error("CHANNEL_SIGN_FAIL,签名失败，请检查证书文件是否存在，密码是否正确");
            SystemError.wrapBs("500", "签名失败");
        }
        return null;
    }


}
